package com.wenjie.hotelsys.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wenjie.hotelsys.domain.Hotel;
import com.wenjie.hotelsys.exception.ApplicationException;
import com.wenjie.hotelsys.service.HotelService;
import com.wenjie.hotelsys.service.HotelServiceImpl;

public class HotelMgrServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String act =request.getParameter("act");
		if ("input".equals(act)) {
			request.getRequestDispatcher("jsps/hotel/input_hotel.jsp").forward(request, response);
		}
		else if("create".equals(act)){
			Hotel hotel=new Hotel();
			hotel.setHotelName(request.getParameter("hotelname"));
			hotel.setHotelAddr(request.getParameter("hoteladdr"));
			hotel.setHotelPhone(request.getParameter("hotelphone"));
			
			HotelService hotelService=new HotelServiceImpl();
			hotelService.createHotel(hotel);
			response.sendRedirect("hotelMgr?act=loadHotel");
		}
		else if ("loadHotel".equals(act)) {
			HotelService hotelService=new HotelServiceImpl();
			request.setAttribute("hotelList", hotelService.loadHotel());
			request.getRequestDispatcher("jsps/hotel/list_hotel.jsp").forward(request, response);
		}
		else if ("remove".equals(act)) {
			int hotelNo=Integer.parseInt(request.getParameter("hotelno"));
			HotelService hotelService=new HotelServiceImpl();
			try {
				hotelService.removeHotel(hotelNo);
			}catch(ApplicationException e){
				request.setAttribute("err", e.getMessage());
				request.setAttribute("hotelList", hotelService.loadHotel());
				request.getRequestDispatcher("jsps/hotel/list_hotel.jsp").forward(request, response);					
			    return;
			}
			response.sendRedirect("hotelMgr?act=loadHotel");
		}
		else if ("update".equals(act)) {
			int hotelNo=Integer.parseInt(request.getParameter("hotelno"));
			HotelService hotelService=new HotelServiceImpl();
			Hotel hotel=hotelService.getHotelByNo(hotelNo);
			hotel.setHotelName(request.getParameter("hotelname"));
			hotel.setHotelAddr(request.getParameter("hoteladdr"));
			hotel.setHotelPhone(request.getParameter("hotelphone"));
			hotelService.updateHotel(hotel);
			response.sendRedirect("hotelMgr?act=loadHotel");
		}
		else if ("forUpdate".equals(act)) {
			int hotelNo=Integer.parseInt(request.getParameter("hotelno"));
			HotelService hotelService=new HotelServiceImpl();
			request.setAttribute("hotel", hotelService.getHotelByNo(hotelNo));
			request.getRequestDispatcher("jsps/hotel/update_hotel.jsp").forward(request, response);
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
